package com.hoshiibuy.sku.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiibuy.common.exception.ServiceException;
import com.hoshiibuy.common.rpc.Response;
import com.hoshiibuy.sku.entity.BaseSkuGallery;
import com.hoshiibuy.sku.entity.param.BaseSkuGalleryQueryParam;
import com.hoshiibuy.sku.entity.param.BaseSkuGalleryUpdateParam;
import com.hoshiibuy.sku.entity.param.BaseSkuGalleryAddParam;
import com.hoshiibuy.sku.service.BaseSkuGalleryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import com.hoshiibuy.common.utils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 基础SKU商品图片资源 控制器
 * </p>
 *
 * @author zhangshuhu
 * @since 2019-05-05
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/sku/base-sku-gallery")
@Api(value = "SKU管理", tags = {"基础SKU商品图片资源管理"})
public class BaseSkuGalleryController {
    @Autowired
    private BaseSkuGalleryService baseSkuGalleryService;

    /**
    * @param param
    * @return com.hoshiibuy.common.rpc.Response
    * @description: 分页查询基础SKU商品图片资源
    */
    @ApiOperation(value = "分页查询基础SKU商品图片资源")
    @PostMapping("/query/page")
    public Response page(@RequestBody @Valid BaseSkuGalleryQueryParam param) {
        try {
            log.info("分页查询基础SKU商品图片资源");
            QueryWrapper<BaseSkuGallery> queryWrapper = new QueryWrapper<BaseSkuGallery>();
            queryWrapper.eq(param.getSkuid() != null, "skuid", param.getSkuid());
            queryWrapper.eq(param.getImageType() != null, "image_type", param.getImageType());
            queryWrapper.eq(StringUtils.isNotBlank(param.getDescription()), "description", param.getDescription());
            queryWrapper.eq(StringUtils.isNotBlank(param.getImage()), "image", param.getImage());
            queryWrapper.eq(param.getDeleted() != null, "deleted", param.getDeleted());
            IPage<BaseSkuGallery> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
            queryWrapper.select(BeanUtils.getSqlSelect(BaseSkuGallery.class));
            return Response.data(baseSkuGalleryService.page(page, queryWrapper));
          } catch (ServiceException e) {
            return Response.failed(e.getError().getMsg());
          } catch (Exception e) {
            log.error("分页查询基础SKU商品图片资源异常", e);
            return Response.failed("分页查询基础SKU商品图片资源异常");
        }
    }
    /**
    * @param id
    * @return com.hoshiibuy.common.rpc.Response
    * @description: 根据id查询基础SKU商品图片资源
    */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询基础SKU商品图片资源")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response queryById(@PathVariable("id") Long id) {
        return Response.data(baseSkuGalleryService.getById(id));
    }

    /**
    * @param param
    * @return com.hoshiibuy.common.rpc.Response
    * @description: 新增
    */
    @PostMapping("/save")
    @ApiOperation(value = "新增基础SKU商品图片资源")
    public Response save(@RequestBody @Valid BaseSkuGalleryAddParam param) {
        try {
            BaseSkuGallery data = BeanUtils.covertType(param, BaseSkuGallery.class);
            return Response.data(baseSkuGalleryService.save(data));
        } catch (ServiceException e) {
            log.error("新增基础SKU商品图片资源异常", e);
            return Response.failed(e.getError().getMsg());
        }catch (Exception e) {
            log.error("新增基础SKU商品图片资源异常", e);
            return Response.failed();
        }
    }

    /**
    * @param param
    * @return com.hoshiibuy.common.rpc.Response
    * @description: 修改基础SKU商品图片资源
    */
    @PutMapping("/update")
    @ApiOperation(value = "修改基础SKU商品图片资源")
    public Response update(@RequestBody @Valid BaseSkuGalleryUpdateParam param) {
        try {
            BaseSkuGallery data = BeanUtils.covertType(param, BaseSkuGallery.class);
            return Response.data(baseSkuGalleryService.updateById(data));
        }catch (ServiceException e) {
            log.error("修改基础SKU商品图片资源异常", e);
            return Response.failed(e.getError().getMsg());
        }catch (Exception e) {
            log.error("修改基础SKU商品图片资源异常", e);
            return Response.failed();
        }
    }
}
